/*package com.adams.action.videoupload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.adams.access.db.bean.ActionType;
import com.adams.action.view.ViewAction;
import com.adams.core.exception.BusinessException;
import com.adams.custom.page.core.BusinessDealFlowContext;
import com.adams.custom.page.core.service.BusinessDealFlowEngineService;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;
import com.adams.utils.FileUtil;
import com.adams.utils.PinyinUtil;
import com.adams.utils.SystemConfig;

public class VideoUploadAction extends ViewAction
{
	Logger logger = Logger.getLogger(VideoUploadAction.class.getName());
	private boolean tooLarge = false;

	private BusinessDealFlowEngineService businessDealFlowEngineService;

	public void setBusinessDealFlowEngineService(
			BusinessDealFlowEngineService businessDealFlowEngineService)
	{
		this.businessDealFlowEngineService = businessDealFlowEngineService;
	}

	*//**
	 * Description : <Detail Description for package> Author : 904370
	 *//*
	private static final long serialVersionUID = -2673542626399269325L;

	public static final String defaultTargetURL = (String) SystemConfig
			.getInstance().getProperty("uploadvideo.dir");;

	static
	{
		File forder = new File(defaultTargetURL);
		if (!forder.exists())
		{
			forder.mkdirs();
		}
	}

	// private UploadVideoService uploadVideoService;

	private File url;// 标题图片
	private String urlFileName;
	private String urlContentType;

	public void setUrl(File url)
	{
		this.url = url;
	}

	public File getUrl()
	{
		return url;
	}

	public String getUrlFileName()
	{
		return urlFileName;
	}

	public void setUrlFileName(String urlFileName)
	{
		this.urlFileName = urlFileName;
	}

	public String getUrlContentType()
	{
		return urlContentType;
	}

	public void setUrlContentType(String urlContentType)
	{
		this.urlContentType = urlContentType;
	}

	// public void setUploadVideoService(UploadVideoService uploadVideoService)
	// {
	// this.uploadVideoService = uploadVideoService;
	// }

	*//**
	 * Description : 视频上传
	 * 
	 * @return
	 * @throws Exception
	 * 
	 *//*
	@SuppressWarnings("unchecked")
	public String saveOrUpdateVidoeResource() throws Exception
	{

		if (tooLarge)
		{
			throw new BusinessException("uploadFile.toolarge.exception");
		}

		Map inputMap = ViewAction.requestParameterMap2Map(request);
		inputMap.remove("url");
		UploadVideoResource uploadVideoResource = (UploadVideoResource) ViewUtil
				.map2Object(inputMap, UploadVideoResource.class);

		if (uploadVideoResource.getId() != null
				&& uploadVideoResource.getId().length() > 0)
		{
			if (url != null)
			{
				throw new BusinessException(
						"uploadFile.URLCannotModify.exception");
			}
			List<Map> inputMaps = new ArrayList<Map>();
			inputMaps.add(inputMap);
			BusinessDealFlowContext context = new BusinessDealFlowContext(
					UploadVideoResource.TABLECODE, ActionType.ACTIONTYPE_ADD,
					inputMaps);
			businessDealFlowEngineService.run(context);
		} else
		{
			if (url == null || url.length() == 0)
			{
				throw new BusinessException("uploadFile.empty.exception");
			}

			if (urlFileName == null || urlFileName.length() == 0)
			{
				throw new BusinessException("uploadFileName.empty.exception");
			}

			String vedioPostfixName = null;

			if (urlFileName.endsWith(".ts") || urlFileName.endsWith(".TS"))
			{
				vedioPostfixName = ".ts";
			} else if (urlFileName.endsWith(".mpg"))
			{
				vedioPostfixName = ".mpg";
			} else
			{
				throw new BusinessException("vedio.format.exception");
			}

			//UUID uuid = UUID.randomUUID();
			//String fileName = uuid + vedioPostfixName;
			
			String fileName = String.valueOf(System.currentTimeMillis());
			String targetFileURL = FileUtil.addPath(defaultTargetURL, fileName + vedioPostfixName);

			if (copyFile(url, targetFileURL))
			{
				uploadVideoResource.setId(fileName);
				uploadVideoResource.setUrl(targetFileURL);
				uploadVideoResource.setCreateTime(DateFormatUtils.format(
						Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss"));

				String targetXMLFileURL = FileUtil.addPath(defaultTargetURL,
						fileName + ".xml");

				if (makeXMLFile(uploadVideoResource, targetXMLFileURL))
				{
					inputMap.put("id", uploadVideoResource.getId());
					inputMap.put("url", uploadVideoResource.getUrl());
					inputMap.put("createtime", uploadVideoResource
							.getCreateTime());
					List<Map> inputMaps = new ArrayList<Map>();
					inputMaps.add(inputMap);
					try
					{
						BusinessDealFlowContext context = new BusinessDealFlowContext(
								UploadVideoResource.TABLECODE,
								ActionType.ACTIONTYPE_EDIT, inputMaps);
						businessDealFlowEngineService.run(context);
					} catch (BusinessException e)
					{
						if (e.getErrorCode()
								.equals("data.has.record.exception"))
						{
							sendFailureMessage("视频名称必须唯一");
							return null;
						}

					}
				}
			}
		}

		Map map = new HashMap();
		map.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	*//**
	 * 替换文件上传中出现的错误信息
	 * *//*
	@Override
	public void addActionError(String anErrorMessage)
	{

		if (anErrorMessage
				.startsWith("the request was rejected because its size"))
		{
			this.tooLarge = true;
		} else
		{
			super.addActionError(anErrorMessage);
		}
	}

	private boolean copyFile(File resource, String targetFileURL)
			throws Exception
	{
		boolean result = true;

		int BUFFER_SIZE = 16 * 1024;
		InputStream in = null;
		OutputStream out = null;

		try
		{
			in = new BufferedInputStream(new FileInputStream(resource),
					BUFFER_SIZE);

			File targetFile = new File(targetFileURL);

			if (targetFile.exists())
			{
				throw new BusinessException("uploadFile.error");
			}

			targetFile.createNewFile();
			out = new BufferedOutputStream(new FileOutputStream(targetFile),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			while (in.read(buffer) > 0)
			{
				out.write(buffer);
			}
		} catch (Exception e)
		{
			result = false;
			e.printStackTrace();
			throw new BusinessException("uploadFile.error");
		} finally
		{
			if (in != null)
			{
				in.close();
			}
			if (out != null)
			{
				out.close();
			}
		}

		return result;
	}

	private boolean makeXMLFile(UploadVideoResource video,
			String targetXMLFileURL) throws Exception
	{
		boolean result = true;

		OutputFormat format = OutputFormat.createPrettyPrint(); //设置XML文档输出格式
		format.setEncoding("UTF-8"); //设置XML文档的编码类型
		format.setSuppressDeclaration(false);
		format.setIndent(true); //设置是否缩进
		format.setIndent(" "); //以空格方式实现缩进
		format.setNewlines(true); //设置是否换行

		XMLWriter xmlWriter = null;
		try
		{
			Document document = DataTransformer.toXml(video);

			File XMLFile = new File(targetXMLFileURL);
			if (XMLFile.exists())
			{
				throw new BusinessException("uploadFile.error");
			}

			XMLFile.createNewFile();
			xmlWriter = new XMLWriter(new FileOutputStream(XMLFile),format);
			xmlWriter.write(document);

		} catch (Exception e)
		{
			result = false;
			e.printStackTrace();
		} finally
		{
			if (xmlWriter != null) {
				xmlWriter.close();
			}
		}

		return result;
	}

	public static Map<String, String> mapPingying = new HashMap<String, String>();

	static
	{
		mapPingying.put("ABCDE", "1");
		mapPingying.put("FGHIJ", "2");
		mapPingying.put("KLMNO", "3");
		mapPingying.put("PQRST", "4");
		mapPingying.put("UVWXYZ", "5");
	}

	// 获得歌手pingying对应的字母
	private static String getPingYingNum(String vpingyins)
	{
		for (String key : mapPingying.keySet())
		{
			if (key.indexOf(vpingyins) > -1)
			{
				return mapPingying.get(key);
			}
		}

		return "6";
	}

	*//**
	 * Description : 增加数据 modify by wangmt 增加自己填充字数，拼音等
	 * 
	 * @return
	 * @throws Exception
	 * 
	 *//*
	@Override
	@SuppressWarnings("unchecked")
	public String addDataByViewForm() throws Exception
	{
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}

		Map inputMap = ViewAction.requestParameterMap2Map(request);

		logger.info("新增KLOK数据开始：" + inputMap);

		String vname = (String) inputMap.get("vname");

		inputMap.put("vfirstonename", PinyinUtil.getPinYinHeadChar(vname));
		inputMap.put("vcharnumber", vname.length());
		inputMap.put("vpingyins", getPingYingNum(PinyinUtil.getPinYin(
				(String) inputMap.get("vsinger")).substring(0, 1)));

		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);

		if (map.get("success") != null
				&& ((Boolean) map.get("success")) == true)
		{
			map.put("msg", "保存成功");
		}

		write(JSONObject.fromObject(map).toString());
		logger.info("新增KLOK数据结束：" + inputMap);
		return null;
	}

	*//**
	 * Description : 更新数据 modify by wangmt 增加自己填充字数，拼音等
	 * 
	 * @return
	 * @throws Exception
	 * 
	 *//*
	@Override
	@SuppressWarnings("unchecked")
	public String updateDataByViewForm() throws Exception
	{
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		logger.info("更新KLOK数据开始：" + inputMap);

		String vname = (String) inputMap.get("vname");
		inputMap.put("vfirstonename", PinyinUtil.getPinYinHeadChar(vname));
		inputMap.put("vcharnumber", vname.length());
		inputMap.put("vpingyins", getPingYingNum(PinyinUtil.getPinYin(
				(String) inputMap.get("vsinger")).substring(0, 1)));

		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		if (map.get("success") != null
				&& ((Boolean) map.get("success")) == true)
		{
			map.put("msg", "保存成功");
		}
		write(JSONObject.fromObject(map).toString());
		logger.info("更新KLOK数据结束：" + inputMap);
		return null;
	}

}
*/